﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;

using Common;

using Castle.Core.Interceptor;
using Castle.DynamicProxy;

namespace com.globalwave.a8ext
{
    /// <summary>
    /// Product 的摘要说明
    /// </summary>
    public class ProductService : BaseService
    {
        private static NameValueCollection whereMap = null;
        private const string TABLE_NAME = "Pub_Ptype";

        public ProductService()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public ProductService(BaseService other)
            : base(other) 
        {
        }
        virtual public DataSet query(
                NameValueCollection criterion,
                int pageIndex,
                int pageSize,
             string orderString)
        {

            NameValueCollection newCriterion = new NameValueCollection();

            if (criterion["sid"] != null || "".Equals(criterion["sid"]))
            {
                newCriterion["@date_start"] = criterion["date_start"];
                newCriterion["@date_end"] = criterion["date_end"];
                newCriterion["@sid"] = criterion["sid"];
            }

            if (criterion["FullName"] != null || "".Equals(criterion["FullName"]))
            {
                newCriterion["FullName"] = "%" + criterion["FullName"] + "%";
            }

            if (criterion["UserCode"] != null || "".Equals(criterion["UserCode"]))
            {
                newCriterion["UserCode"] = "%" + criterion["UserCode"] + "%";
            }

            if (newCriterion["@sid"] == null)
            {
                return dao.queryTable(TABLE_NAME, pageIndex, pageSize, newCriterion, orderString == null ? " order by UserCode " : orderString);
            }
            else {
                return dao.query(ProductCriterion.GET_ALL_PRODUCT_4_REWARD, pageIndex, pageSize, newCriterion, orderString == null ? " order by UserCode " : orderString, "Product");
            }
        }

        virtual public DataSet query4J2ME(NameValueCollection criterion, string imei)
        {
            /*
            if (imei == null) {
                return null;
            }*/
            return dao.query(ProductCriterion.GET_ALL_PRODUCT_4_J2ME, 1, 5000, criterion, null, "Product");
        }

        virtual public DataSet query4OrderPrintint(NameValueCollection criterion, string imei)
        {
            /*
            if (imei == null) {
                return null;
            }*/
            return dao.query(ProductCriterion.GET_ALL_PRODUCT_4_ORDER_PRINTING, 1, 5000, criterion, null, "Product");
        }


        virtual public int create(NameValueCollection product)
        {
            return dao.insertTable(TABLE_NAME, product);
        }

        virtual public int update(NameValueCollection product)
        {
            NameValueCollection criterion = new NameValueCollection();
            string id = product.Get("_id");

            criterion.Add("_id", id);

            product.Remove("_id");

            NameValueCollection updateObject = new NameValueCollection();
            updateObject.Add("ext_num_1", product["ext_num_1"]);
            updateObject.Add("ext_num_2", product["ext_num_2"]);

            int result = dao.updateTable(TABLE_NAME, updateObject, criterion);

            product.Add("_id", id);

            return result;
        }

        virtual public int delete(NameValueCollection criterion)
        {
            if (criterion.Get("product_id") != null)
            {
                throw new BaseException("E000001", "参数错误");
            }
            // delete cascade 
            //dao.deleteTable(TABLE_BULLETIN, criterion);
            //dao.deleteTable(TABLE_TOPIC, criterion);
            
            return dao.deleteTable(TABLE_NAME, criterion);

        }

        virtual public void deleteAll(string[] productIds)
        {
            if (productIds == null || productIds.Length == 0)
            {
                throw new BaseException("", "parames");
            }

            NameValueCollection criterion = new NameValueCollection();
            foreach (string productId in productIds)
            {
                criterion.Add("product_ids", productId);
            }

            // delete cascade
            //dao.deleteTable(TABLE_BULLETIN, criterion);
            //dao.deleteTable(TABLE_TOPIC, criterion);

            dao.deleteTable(TABLE_NAME, criterion);

        }

        public NameValueCollection getWhereConfigMap()
        {
            if (whereMap != null)
            {
                return whereMap;
            }

            whereMap = new NameValueCollection();

            whereMap.Add("product_ids", " product_id in (@product_ids) ");
            whereMap.Add("FullName", " FullName like ? ");
            whereMap.Add("UserCode", " UserCode like ? ");

            return whereMap;
        }

        public static ProductService getInstance()
        {

            ProxyGenerator generator = new ProxyGenerator();

            return generator.CreateClassProxy<ProductService>(new TransactionInterceptor());

        }
    }


    public class ProductCriterion
    {
        // eff<=end && exp>= start
        //           start           end 
        // ------------|--------------|--------------------
        //       |--------||-------||---------|
        //       eff   exp  eff  exp eff     exp
        public const string GET_ALL_PRODUCT_4_REWARD = "select p.*,r.* from pub_ptype p left join " +
            " (select * from a8ext_reward where exp_date >= ? and eff_date <= ? and sid=?) r " +
            " on p._id = r.pid where 1=1 ";

        public const string GET_ALL_PRODUCT_4_J2ME = "select p.fullname, p.usercode, cast(pp.prebuyprice as int) as price1, cast(pp.preprice1 as int) as price2, cast(pp.preprice2 as int) as price3 from pub_ptype p, pub_ptype_price pp where p._id = pp.pid and p.soncount = 0";

        public const string GET_ALL_PRODUCT_4_ORDER_PRINTING = "select * from pub_ptype p";
   
        
    }
}
